package com.whfc.ms.api.fmam.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fmam.entity.dto.FmamMatDTO;
import com.whfc.fmam.entity.dto.FmamMatImportResultDTO;
import com.whfc.fmam.entity.dto.FmamMatTypeDTO;
import com.whfc.fmam.entity.qo.MatAddQO;
import com.whfc.fmam.entity.qo.MatEditQO;
import com.whfc.fmam.entity.qo.MatTypeAddQO;
import com.whfc.fmam.entity.qo.MatTypeEditQO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.fmam.service.MatService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author sun_guodong
 * @description 材料字典
 * @date 2019-11-18
 */
@RequiresPermissions("fmam/mat")
@RestController
@RequestMapping("/ms/api/fmam")
public class FmamMatController extends BaseController {
    @Autowired
    private MatService matService;

    /**
     * 获取材料类别列表
     *
     * @param user
     * @param deptId
     * @param keyword
     * @return
     */
    @GetMapping("/matType/list")
    public Result matTypeList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @RequestParam("deptId") Integer deptId,
                              @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("获取材料类别列表,loginer:{},deptId:{},keyword:{}", user.getUsername(), deptId, keyword);
        ListData<FmamMatTypeDTO> listData = matService.matTypeList(deptId, keyword);
        return ResultUtil.success(listData);
    }

    @GetMapping("/matType/option/list")
    public Result matTypeOptionList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                    @RequestParam("deptId") Integer deptId) {
        logger.info("获取所有材料类别列表,loginer:{},deptId:{}", user.getUsername(), deptId);
        ListData<FmamMatTypeDTO> listData = matService.matTypeOptionList(deptId);
        return ResultUtil.success(listData);
    }

    @RequiresPermissions("fmam/matType/add")
    @PostMapping("/matType/add")
    public Result matTypeAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestBody @Validated MatTypeAddQO request) {
        logger.info("新增材料类别，loginer:{},MatTypeAddQO:{}", user.getUsername(), request.toString());
        matService.matTypeAdd(request);
        return ResultUtil.success();
    }

    @RequiresPermissions("fmam/matType/edit")
    @PostMapping("/matType/edit")
    public Result matTypeEdit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @RequestBody @Validated MatTypeEditQO request) {
        logger.info("编辑材料类别，loginer:{},MatTypeEditQO:{}", user.getUsername(), request.toString());
        matService.matTypeEdit(request);
        return ResultUtil.success();
    }

    @RequiresPermissions("fmam/matType/del")
    @PostMapping("/matType/del/{id}")
    public Result matTypeDel(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @PathVariable("id") Long id) {
        logger.info("删除材料类别，loginer:{},id:{}", user.getUsername(), id);
        matService.matTypeDel(id);
        return ResultUtil.success();
    }


    @GetMapping(value = "/mat/list", params = {"pageNum", "pageSize"})
    public Result matList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("matTypeId") Long matTypeId,
                          @RequestParam("pageNum") Integer pageNum,
                          @RequestParam("pageSize") Integer pageSize,
                          @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("材料列表，loginer:{},matTypeId:{},pageNum:{},pageSize:{},keyword:{}",
                user.getUsername(), matTypeId, pageNum, pageSize, keyword);
        PageData<FmamMatDTO> pageData = matService.matList(matTypeId, pageNum, pageSize, keyword);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/mat/list")
    public Result matList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("deptId") Integer deptId,
                          @RequestParam(value = "matTypeId", required = false) Long matTypeId) {
        logger.info("获取所有的材料列表,loginer:{},deptId:{},matTypeId:{}", user.getUsername(), deptId, matTypeId);
        ListData<FmamMatDTO> listData = matService.matList(deptId, matTypeId);
        return ResultUtil.success(listData);
    }

    @RequiresPermissions("fmam/mat/add")
    @PostMapping("/mat/add")
    public Result matAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestBody @Validated MatAddQO request) {
        logger.info("新增材料，loginer:{},MatAddQO:{}", user.getUsername(), request.toString());
        matService.matAdd(request);
        return ResultUtil.success();
    }

    @RequiresPermissions("fmam/mat/edit")
    @PostMapping("/mat/edit")
    public Result matEdit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestBody @Validated MatEditQO request) {
        logger.info("编辑材料，loginer:{},MatEditQO:{}", user.getUsername(), request.toString());
        matService.matEdit(request);
        return ResultUtil.success();
    }

    @RequiresPermissions("fmam/mat/del")
    @PostMapping("/mat/del/{id}")
    public Result matDel(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @PathVariable("id") Long id) {
        logger.info("删除材料，loginer:{},id:{}", user.getUsername(), id);
        matService.matDel(id);
        return ResultUtil.success();
    }

    @PostMapping("/mat/importExcel")
    public Result matImport(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestParam Integer deptId,
                            MultipartFile file) throws IOException {
        logger.info("批量导入材料，loginer:{},deptId:{},参数:{}", user.getUsername(), deptId, file.toString());
        FmamMatImportResultDTO matImportResultDTO = matService.importExcel(deptId, file);
        return ResultUtil.success(matImportResultDTO);
    }


}
